﻿package  {
import com.urbansquall.serenity.*;
import flash.display.*;
import flash.filters.GlowFilter;

public class Entity extends Sprite implements IEntity 
{
	private static var s_idCounter : SimpleIDCounter = new SimpleIDCounter();
	
	private var m_id : String;
	private var m_main : Main;
	
	private var m_vx : Number;
	private var m_vy : Number;
	
	public function Entity( a_main : Main ) 
	{
		m_main = a_main;
		m_id = s_idCounter.calculateNextID();
		
		var color : uint = Math.random() * 0xFFFFFF;
		var shape : Shape = new Shape();
		shape.graphics.beginFill( color );
		shape.graphics.drawCircle( 0, 0, 2 );
		shape.filters = [ new GlowFilter( color ) ];
		
		var bitmapData : BitmapData = new BitmapData( 10, 10 );
		bitmapData.draw( shape );
		var bitmap : Bitmap = new Bitmap( bitmapData );
		addChild( shape );
		
		this.cacheAsBitmap = true;
		this.mouseEnabled = false;
		this.mouseChildren = false;
		
		this.x = 0;
		this.y = 0;

		m_vx = (color / 0xFFFFFF) / 10;
		m_vy = 0.01;
	}	
	
	public function dispose():void {}
	
	public function get id() : String { return m_id; }	
		
	public function tick( a_deltaTime : Number ) : void
	{			
		var xSeparationToMouse : Number = m_main.mouseX - this.x;		
		var ySeparationToMouse : Number = m_main.mouseY - this.y;
		
		m_vy += ySeparationToMouse / 200000;
		
		this.x += a_deltaTime * m_vx;
		this.y += a_deltaTime * m_vy;
		
		if( this.x > this.stage.stageWidth + this.width )
		{
			m_main.killEntity( this );
		}
	}	
	
	override public function toString() : String { return "<Entity:" + id + ">"; }
	
}	
}